unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, rtcInfo, rtcConn, rtcTcpCli, RzEdit, ExtCtrls, RzPanel,
  Mask, OleServer, RzSplit, OleCtrls, SIGNXLib_TLB;

type
  TForm1 = class(TForm)
    Btnx_1: TButton;
    RzMemo1: TRzMemo;
    RzMemo2: TRzMemo;
    RzGroupBox1: TRzGroupBox;
    RzGroupBox2: TRzGroupBox;
    RzToolbar1: TRzToolbar;
    Edit_ServAddr: TRzEdit;
    Edit_ServPort: TRzEdit;
    Button1: TButton;
    RzMemo3: TRzMemo;
    RzSplitter1: TRzSplitter;
    Button2: TButton;
    SignX1: TSignX;
    Button3: TButton;
    Button4: TButton;
    procedure Btnx_1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RtcTcpClient2DataReceived(Sender: TRtcConnection);
    procedure RtcTcpClient2Disconnect(Sender: TRtcConnection);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FTotal:Integer;
    FReadText:string;
    RtcTcpClient2:TRtcTcpClient;
    FSelfBase64:string;
    FBankBase64:string;
    FKeyCs     :string;
  public
    function Send:string;
  end;

var
  Form1: TForm1;

const
sendtxt1='00004201.00330212   3940201006201303012013033139402001040021711018001   ';
sendtxt2='00356011.00330212   3940201005TG3uNQGCl4KeGq9xtmxI2zJc7R/vxYgLZrONGSymFqRAcsI5F+69tZg+unGOcoM4jWI31iKJp/UxN/rJEDG44xcBh/LeYTldIIM9Zf/dxC6Ghe59v5Owmlv3jjXE26C2eSpHEXHHCQPpNc80aSpwkJBR3ljZiv22lVQHAAvkoc'
        +'uvDSOu5eLpTw1Ezgv5CNA75Xeb7mD3Nv5GbH2X6RGZjwRSggMIw9ErMoGL+19dtkd26ms6UW1AkXMgsO3kDHwdhQrNxqDpwHVCIbQD1DPvKwSKlXhqSZ+MdgmRkexFngDOJ+UyrryVlfFTPF6oPj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/qiA+9rl1WAxEqcshrp8/tcMxeD/NaP/Agjbr5o+YDMB4dVy7VrFjFAv/XrVsUm4rJQ5h2dSqKnu3rw/H5GjGHiKWeVJkBstPVc9ARRnQsmrXbfgyuMr3pKJhd+PlVgjNWntGOkFNciSY/PKtDD9MgqIhxrKBuVtv0ulE6'
        +'uV4lDF9EfV+O3QrQZQfzOY9OwXVp/gdCPZOMKHOCmKhbRZcgeJIA6DvcY1DKJ+ToL/i6E1UwcKzTkwTrJKbqlhiNGwO6WGrw/aUiwq239kZlKSz5uhNWFxcHfJPDe+PTo1WNNQf7fDa4Spa3eRgh4+9Lk941s4YulXIVTjM/JVVqZ++LnBPjx2nkKZrV2RPnFoDGzEaT'
        +'uS2VlS3buxLMEP4InEamk=MIIB2QYJKoZIhvcNAQcDoIIByjCCAcYCAQAxggF+MIIBegIBADCB4jCByDELMAkGA1UEBhMCQ04xHTAbBgNVBAoMFGlUcnVzY2hpbmEgQ28uLCBMdGQuMRwwGgYDVQQLDBNDaGluYSBUcnVzdCBOZXR3b3JrMUAwPgYDVQQLDDdUZXJtcy'
        +'BvZiB1c2UgYXQgaHR0cHM6Ly93d3cuaXRydXMuY29tLmNuL2N0bnJwYSAoYykyMDA3MTowOAYDVQQDDDFpVHJ1c2NoaW5hIENOIEVudGVycHJpc2UgSW5kaXZpZHVhbCBTdWJzY3JpYmVyIENBAhUA+zGeas/zuUiJgNla6JEHUbsPCQAwDQYJKoZIhvcNAQEBBQAEgY'
        +'CkoQnf05w+P0t1tw+HcHlvINUz3sUfcsGy7vYCyHS0Z56Mhbz4RxzIDueJikizwzc7RqLx3Rt0oL5+XDsqEX3qwDY+HbWUli5AR0h1J0uVbNIrCbhNcD50RDWHpMlDh+Iq+LLNNTyGq2TgqB9x0vRi79ZNBlHxjMjiSSk/7mjqRzA/BgkqhkiG9w0BBwEwDAYIKoZIhv'
        +'cNAwQFAIAknkVzHtdGkeEjJUeUObJRPtNZThoIwubsAFQ6rTZCGY/41uJ6  ';
                                                                                         
sendtxt3='00209311.00330212   394020100539402001040021711018001   ';

bankbase64='MIIEETCCA3qgAwIBAgIVAPsxnmrP87lIiYDZWuiRB1G7DwkAMA0GCSqGSIb3DQEBBQUAMIHIMQswCQYDVQQGEwJDTjEdMBsGA1UECgwUaVRydXNjaGluYSBDby4sIEx0ZC4xHDAaBgN'
          +'VBAsME0NoaW5hIFRydXN0IE5ldHdvcmsxQDA+BgNVBAsMN1Rlcm1zIG9mIHVzZSBhdCBodHRwczovL3d3dy5pdHJ1cy5jb20uY24vY3RucnBhIChjKTIwMDcxOjA4BgNVBAMMMWlUcn'
          +'VzY2hpbmEgQ04gRW50ZXJwcmlzZSBJbmRpdmlkdWFsIFN1YnNjcmliZXIgQ0EwHhcNMTMwMTI5MDAwMDAwWhcNMTQwMTI4MjM1OTU5WjCCATcxJzAlBgNVBAoeHlkpWgGL2k/hZXBbV'
          +'4uki8FOLV/DW4Fs4lIGTi1fwzEzMDEGA1UEAx4qkR5d3k5hlUdRbFKhU2FlL07YfPt+321Li9WLwU5mAC0AdABlAHMAdAAxMR0wGwYJKoZIhvcNAQkBFg5jYUBuYmNhLmNvbS5jbjEN'
          +'MAsGA1UEBh4EAEMATjENMAsGA1UEBx4EW4Fs4jENMAsGA1UECB4EbVlsXzEdMBsGA1UEAR4UADEAMgAzADQANQA2ADcAOAAtADkxGTAXBgNVBBQeEAA4ADcAMwAwADQANQAwADAxGzA'
          +'ZBgNVBAseEgAwADAAMAAyAC0AMAAwADAAMTEZMBcGA1UECx4QAE4AUwBSAFUASQBEADoAMDEZMBcGA1UECx4QAEQARQBQAFUASQBEADoAMDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgY'
          +'kCgYEAvq7KM8y1mbLhvbbmnnYdezcYZLOcFk3dNTiSJhfULSA5cYVjET/3N5CthvfS/VI5dv1DIfVB9OTvRVWufMlTdXJacaQrocvaJIzaOjyJdjRNP8/Q+5S/N5pyAoexPV6Z6aFmM'
          +'q2oHZb7r/gf32xQe1HRNX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VlsXzEd'
+'MBsGA1UEAR4UADEAMgAzADQANQA2ADcAOAAtADkxGTAXBgNVBBQeEAA4ADcAMwAwADQANQAwADAxGzAZBgNVBAseEgAwADAAMAAyAC0AMAAwADAAMTEZMBcGA1UECx4QAE4A'
+'UwBSAFUASQBEADoAMDEZMBcGA1UECx4QAEQARQBQAFUASQBEADoAMDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAvq7KM8y1mbLhvbbmnnYdezcYZLOcFk3dNTiSJhfU'
+'LSA5cYVjET/3N5CthvfS/VI5dv1DIfVB9OTvRVWufMlTdXJacaQrocvaJIzaOjyJdjRNP8/Q+5S/N5pyAoexPV6Z6aFmMq2oHZb7r/gf32xQe1HRNX0KoKWokQVSQwYFxa0C'
+'AwEAAaOBhDCBgTAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDBnBgNVHR8EYDBeMFygWqBYhlZodHRwOi8vaWNhLXB1YmxpYy5pdHJ1cy5jb20uY24vY2dpLWJpbi9pdHJ1c2Ny'
+'bC5wbD9DQT03RDIzMDcyMzc4NTY2MkJGOUQyRTdBNTU0MkU3MEJDQjANBgkqhkiG9w0BAQUFAAOBgQAsq4QsD7FL3JMuzwUX9sftCraU/UkAMrN5fNCwGWAkfLin7+unrPvA'
+'qI/Pu66YR/Icw9fuc5JFcDWGXj25EwIa2a/OHGLdsFmBVIq/4QgRlR5IY56fMypXcbL/hqw55mUsWVCCfznn6qcjK9IdfZaKyb7T7TapW02ElRUzMnqj7Q==';


implementation

uses
  Class_KzUtils;



{$R *.dfm}

procedure TForm1.Btnx_1Click(Sender: TObject);
var
  ReadText:string;
  ReadNumb:Integer;
  LinkStat:Integer;
  ReadStat:Integer;
begin
  FReadText:='';
  if RtcTcpClient2<>nil then
  begin
    FreeAndNil(RtcTcpClient2);
  end;
  RtcTcpClient2:=nil;
  RtcTcpClient2:=TRtcTcpClient.Create(nil);
  RtcTcpClient2.OnDataReceived:=RtcTcpClient2DataReceived;
  RtcTcpClient2.OnDisconnect  :=RtcTcpClient2Disconnect;


  RtcTcpClient2.ServerAddr:=Trim(Edit_ServAddr.Text);
  RtcTcpClient2.ServerPort:=Trim(Edit_ServPort.Text);
  RtcTcpClient2.Connect();
  RtcTcpClient2.Write(sendtxt1);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
//  Edit_ServAddr.Text:='10.10.11.56';
//  Edit_ServPort.Text:='17763';

//  Edit_ServAddr.Text:='127.0.0.1';
//  Edit_ServPort.Text:='1861';

  RzMemo3.Clear;
  RzMemo3.Lines.Add(sendtxt2);
end;

procedure TForm1.RtcTcpClient2DataReceived(Sender: TRtcConnection);
begin
  FReadText:=FReadText+Sender.Read;
end;

procedure TForm1.RtcTcpClient2Disconnect(Sender: TRtcConnection);
begin
  //RzMemo2.Lines.Clear;
  RzMemo2.Lines.Add(Format('recv:%D',[Length(FReadText)]));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ReadText:string;
  ReadNumb:Integer;
  LinkStat:Integer;
  ReadStat:Integer;
  SendText:string;
begin
  if SignX1.SelectCert=0 then
  begin
    FSelfBase64:=SignX1.GetCurrentUsedCertInfo(9);
    FKeyCs     :=SignX1.GetCurrentUsedCertInfo(4);
  end;
  
  FReadText:='';
  if RtcTcpClient2<>nil then
  begin
    FreeAndNil(RtcTcpClient2);
  end;    
  RtcTcpClient2:=TRtcTcpClient.Create(nil);
  RtcTcpClient2.OnDataReceived:=RtcTcpClient2DataReceived;
  RtcTcpClient2.OnDisconnect  :=RtcTcpClient2Disconnect;


  RtcTcpClient2.ServerAddr:=Trim(Edit_ServAddr.Text);
  RtcTcpClient2.ServerPort:=Trim(Edit_ServPort.Text);
  RtcTcpClient2.Connect();
  SendText:=Send;
  RtcTcpClient2.Write(SendText);
end;

function TForm1.Send: string;
var
  Head:string;
  Body:string;
  TextA:string;
  TextB:string;
  Crypt:string;
  Pswd:string;
  NumbA:Integer;
  LengA:string;
  Envelop:string;

  function SignUserData(AOriginData: string): string;
  var
    HashedData:string;
  begin
    if SignX1.SetCertBySN(FKeyCs)=0 then
    begin
      if SignX1.HashData(AOriginData,1)=0 then
      begin
        HashedData:=SignX1.GetHashResult;
        if SignX1.Sign(HashedData,1)=0 then
        begin
          Result:=SignX1.GetSignResult;
        end;  
      end;
    end;
  end;
  
  function ExptCA:string;
  var
    Base64:string;
    OriginData:string;
    SignResult:string;
    DateX:string;
  begin
    Base64:=TKzUtils.TryFormatCode(1792,1,' ',FSelfBase64);
    SignResult:=SignUserData(Body);
    SignResult:=TKzUtils.TryFormatCode(256,1,' ',SignResult);    
    DateX:=FormatDateTime('YYYY-MM-DD HH:MM:SS',Now);
    Result:=Format('%S%S%S',[Base64,SignResult,DateX]);
  end;  
begin
  TextA:='00209311.00330212   394020100539402001040021711018001   ';
  Body :='39402001040021711018001   ';
  TextA:=TextA+ExptCA;
  
  begin
    Head:=Copy(TextA,1,30);
    Body:=Copy(TextA,31,MaxInt);

    SignX1.EnCrypt(Body,'',1);
    Crypt:=SignX1.GetEnCryptResult;
    Pswd :=SignX1.GetLastCryptPassword;

    //BankA.BankBase64
    SignX1.SealEnvelope(Pswd,bankbase64,0);
    Envelop:=SignX1.GetSealEnvelopeData;
    Envelop:=TKzUtils.TryFormatCode(768,1,' ',Envelop);
    TextB:=Format('%S%S%S',[Head,Crypt,Envelop]);

    //YXC_2013_03_13_14_48_50_<
    NumbA :=Length(TextB)-30;
    Delete(TextB,1,6);
    LengA :=TKzUtils.TryFormatCode(6,0,'0',IntToStr(NumbA));
    TextB :=LengA+TextB;
    //YXC_2013_03_13_14_48_57_>
  end;

  Result:=TextB;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  ReadText:string;
  ReadNumb:Integer;
  LinkStat:Integer;
  ReadStat:Integer;
  SendText:string;
begin
  {ShowMessageFmt('%D',[Length(RzMemo3.Text)]);
  exit;}
  FReadText:='';
  if RtcTcpClient2<>nil then
  begin
    FreeAndNil(RtcTcpClient2);
  end;
  RtcTcpClient2:=nil;
  RtcTcpClient2:=TRtcTcpClient.Create(nil);
  RtcTcpClient2.OnDataReceived:=RtcTcpClient2DataReceived;
  RtcTcpClient2.OnDisconnect  :=RtcTcpClient2Disconnect;


  SendText:=TKzUtils.TryFormatCode(1836,1,' ',txt1007);
  RtcTcpClient2.ServerAddr:=Trim(Edit_ServAddr.Text);
  RtcTcpClient2.ServerPort:=Trim(Edit_ServPort.Text);
  RtcTcpClient2.Connect();
  RtcTcpClient2.Write(SendText);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  RzMemo2.Lines.Clear;
  RzMemo2.Lines.Add(FReadText);
end;

end.
